package com.swz.dcrm.ui.monthly;


import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.core.TableConfig;
import com.bin.david.form.data.CellInfo;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.format.bg.BaseCellBackgroundFormat;
import com.bin.david.form.data.format.bg.IBackgroundFormat;
import com.bin.david.form.data.format.bg.ICellBackgroundFormat;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.table.TableData;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.AnalyzeViewModel;
import com.swz.dcrm.util.Tool;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class TableFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;

    @BindView(R.id.table)
    SmartTable smartTable;
    @Inject
    AnalyzeViewModel analyzeViewModel;

    public static TableFragment newInstance() {

        return new TableFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_table;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        TableConfig tableConfig = smartTable.getConfig();
        tableConfig.setShowXSequence(false);
        tableConfig.setShowYSequence(false);
        tableConfig.setShowColumnTitle(true);
        smartTable.setZoom(false);
        FontStyle fontStyle = new FontStyle();
        fontStyle.setTextSpSize(getContext(), 14);
        tableConfig.setContentStyle(fontStyle);
        tableConfig.setColumnTitleBackground(new IBackgroundFormat() {
            @Override
            public void drawBackground(Canvas canvas, Rect rect, Paint paint) {
                paint.setColor( Color.parseColor("#fbfcfd"));
                paint.setStyle(Paint.Style.FILL);
                canvas.drawRect(rect, paint);
            }
        });
        tableConfig.setColumnTitleHorizontalPadding(0);

        tableConfig.setColumnTitleVerticalPadding(Tool.dip2px(getActivity(), 10));
        tableConfig.setColumnTitleStyle(new FontStyle() {
            @Override
            public int getTextColor() {
                return ContextCompat.getColor(getContext(), R.color.gary_6f777d);
            }

            @Override
            public int getTextSize() {
                return Tool.dip2px(getActivity(), 14);
            }
        });

        tableConfig.setShowTableTitle(false);


        ICellBackgroundFormat<CellInfo> backgroundFormat = new BaseCellBackgroundFormat<CellInfo>() {
            @Override
            public int getBackGroundColor(CellInfo cellInfo) {

                if(cellInfo.row%2 == 0){
                    return Color.parseColor("#ffffff");
                }else if(cellInfo.row%2 != 0){
                    return Color.parseColor("#fbfcfd");
                }
                return Color.parseColor("#ffffff");
            }

            @Override
            public int getTextColor(CellInfo cellInfo) {
                if(cellInfo.row==1){
                    return ContextCompat.getColor(getContext(), R.color.gary_6f777d);
                }
                return ContextCompat.getColor(getContext(), R.color.black_2f3234);

            }
        };
        tableConfig.setMinTableWidth(Tool.getScreenWidth(getContext())-Tool.dip2px(getActivity(),20));
        tableConfig.setContentCellBackgroundFormat(backgroundFormat);

        tableConfig.setVerticalPadding(Tool.dip2px(getActivity(), 10));
        TableFragmentArgs tableFragmentArgs=TableFragmentArgs.fromBundle(getArguments());
        title.setText(tableFragmentArgs.getTitle());
        List<String> titles = Arrays.asList(getResources().getStringArray(R.array.analysis_type11));
        Column column1 = new Column(titles.get(0), "carModelName");
        Column column2 = new Column(titles.get(1), "count");
        Column column3 = new Column(titles.get(2), "rate");
        column3.setFormat(o -> o.toString() + "%");
        if(tableFragmentArgs.getTitle().equals(getString(R.string.monthly_structure1))){


            smartTable.setTableData(new TableData("", analyzeViewModel.getMonthlyStructure().getValue().getData().getMonthNewAddList()
                    ,  column1, column2, column3));
        }else if(tableFragmentArgs.getTitle().equals(getString(R.string.monthly_structure2))){

            smartTable.setTableData(new TableData("", analyzeViewModel.getMonthlyStructure().getValue().getData().getMonthOrderCarList()
                    ,  column1, column2, column3));
        }else if(tableFragmentArgs.getTitle().equals(getString(R.string.monthly_structure3))){

            smartTable.setTableData(new TableData("", analyzeViewModel.getMonthlyStructure().getValue().getData().getMonthDeliveryCarList()
                    ,  column1, column2, column3));
        }
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
}
